package dosgi.core.util;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * Helper class which cache the service infos saved in database.
 * 
 * @author Jaroslaw Janik
 */
public class ServicesStorage {

	private static Map<String, ExtendedServiceInfo> references = Collections.synchronizedMap(new HashMap<String, ExtendedServiceInfo>());
	
	/**
	 * Puts given service info into the map.
	 * 
	 * @param reference Reference of service saved in database.
	 * @param serviceInfo Service info saved in database with corresponding id.
	 */
	public static void put(String reference, ExtendedServiceInfo serviceInfo) {
		references.put(reference, serviceInfo);
	}
	
	/**
	 * Removes object with given service reference.
	 * 
	 * @param reference Reference to be removed from storage.
	 */
	public static void remove(String reference) {
		references.remove(reference);
	}
	
	/**
	 * Returns service info saved in database with corresponding id of given service reference.
	 * 
	 * @param reference Reference of service saved in database.
	 * @return Service info saved in database with corresponding id.
	 */
	public static ExtendedServiceInfo get(String reference) {
		return references.get(reference);
	}
	
	/**
	 * Returns of services info saved in database by bundle.
	 * 
	 * @return List of service infos saved in database with corresponding id.
	 */
	public static Collection<ExtendedServiceInfo> values() {
		return references.values();
	}
}
